package me.sdevil507.systemAssist.systemMessage.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.supports.shiro.helper.ServiceAccountHelper;
import me.sdevil507.systemAssist.Constant.SystemAssistConstant;
import me.sdevil507.systemAssist.systemMessage.dto.SystemMessageParamsDto;
import me.sdevil507.systemAssist.systemMessage.dto.SystemMessageReceiverDto;
import me.sdevil507.systemAssist.systemMessage.dto.SystemMessageResultDto;
import me.sdevil507.systemAssist.systemMessage.mapper.SystemMessageTextMapper;
import me.sdevil507.systemAssist.systemMessage.po.SystemMessageRelPo;
import me.sdevil507.systemAssist.systemMessage.service.SystemMessageService;
import me.sdevil507.supports.jpa.PageConvertCallBack;
import me.sdevil507.supports.jpa.PageResultDTO;
import me.sdevil507.supports.jpa.PageResultGenerator;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 日志Controller
 *
 */
@RestController
@RequestMapping("/api/org/systemMessage")
@Slf4j
public class SystemMessageOrgController {

    @Autowired
    private SystemMessageService systemMessageService;
    @Autowired
    private SystemMessageTextMapper systemMessageTextMapper;

    @PostMapping("/getMessageData")
    public ApiResultDTO getMessageData(@RequestBody SystemMessageParamsDto paramsDto) {
        ApiResultDTO apiResultDto;
        try {
            Long deptId = ServiceAccountHelper.getOrgId();
            Long userId = ServiceAccountHelper.getUserId();
            //查询是否有待查看消息
            List<SystemMessageResultDto> result = systemMessageService.getMessageData(SystemAssistConstant.systemAssistTerminal.ORG, deptId, userId);

            apiResultDto = ApiResultGenerator.create("0", "数据获取成功!", result);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            e.printStackTrace();
            apiResultDto = ApiResultGenerator.create("-1", "数据获取失败!");
        }
        return apiResultDto;
    }

    @PostMapping("/v1/readAllByPage")
    public ApiResultDTO readAllByPage(@RequestBody SystemMessageParamsDto paramsDto) {
        ApiResultDTO apiResultDto;
        try {
            Page<SystemMessageRelPo> pagePos = systemMessageService.readAllByPage(paramsDto.getSearchDto(), paramsDto.getPageDto(), paramsDto.getSortDto());

//            PageResultDTO<SystemMessageResultDto> pageResultDto = (PageResultDTO<SystemMessageResultDto>) PageResultGenerator.convert(pagePos, new PageConvertCallBack<SystemMessageResultDto, SystemMessageRelPo>() {
//                @Override
//                public List<SystemMessageResultDto> exec(List<SystemMessageRelPo> list) {
//                    return systemMessageTextMapper.pos2dtos(list);
//                }
//            });
            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", "");
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            e.printStackTrace();
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }

}
